package com.rrd.dw.mr.gzip.kafka;

import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.duxiaoman.DuXiaoManCreditScore;
import com.ucredit.babel.common.model.duxiaoman.DuXiaoManCreditScoreData;
import com.ucredit.babel.common.model.duxiaoman.DuXiaoManCreditScoreInfo;
import com.ucredit.babel.common.model.duxiaoman.DuXiaoManCreditScoreRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 度小满小满分
 *
 * @author renzhanzheng
 */

public class DuXiaoManCreditScoreParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 度小满小满分
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        DuXiaoManCreditScoreRecord obj = (DuXiaoManCreditScoreRecord)model;
        DuXiaoManCreditScoreData data = obj.getData() == null ? new DuXiaoManCreditScoreData() : obj.getData();
        DuXiaoManCreditScore result = data.getResult() == null ? new DuXiaoManCreditScore() : data.getResult();
        DuXiaoManCreditScoreInfo scoreInfo = result.getScoreInfo() == null ? new DuXiaoManCreditScoreInfo() : result.getScoreInfo();

        StringBuilder sb = getBaseInfo(obj, successful);

        //错误码
        sb.append(data.getRetCode()).append(CRT001);
        //错误话术
        sb.append(data.getRetMsg() == null ? "" : data.getRetMsg()).append(CRT001);
        //小满分分值
        sb.append(scoreInfo.getScore() == null ? "" : scoreInfo.getScore()).append(CRT001);
        //小满分模型
        sb.append(scoreInfo.getModels() == null ? "" : scoreInfo.getModels()).append(CRT001);
        StringBuilder txSb = getBaseInfo(obj);
        key.set(getKey());
        val.set((sb.toString() + txSb.toString()).replace("\n","").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_duxiaoman_score_inc";
    }

    @Override
    public String getType() {
        return RecordType.DUXIAOMAN_CREDIT_SCORE_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "度小满小满分";
    }
}
